package
{
	import fl.controls.TextArea;
	import fl.controls.Button;
	import fl.controls.TextInput;
	import flash.display.Sprite;
	import flash.events.SyncEvent;
	import flash.events.NetStatusEvent;
	import flash.events.MouseEvent;
	import flash.net.SharedObject;
	import flash.net.NetConnection;
	
	public class TextSO extends Sprite
	{
		private var button:Button;
		private var text_so:SharedObject;
		private var nc:NetConnection;
		private var textArea:TextArea;
		private var textInput:TextInput;
		private var rtmpGo:String;
		private var good:Boolean;
		
		public function TextSO ()
		{
			//Set up UIs
			textArea=new TextArea();
			textArea.setSize (200,300);
			textArea.move (20,20);
			addChild (textArea);
			
			textInput=new TextInput();
			textInput.move (20,330);
			addChild (textInput);
			
			button=new Button();
			button.width=50;
			button.label="Send";
			button.move (125,330);
			button.addEventListener (MouseEvent.CLICK,sendMsg);
			addChild (button);
			
			rtmpGo = "rtmp://192.168.1.209/test";
			nc = new NetConnection( );
			nc.connect (rtmpGo);
			nc.addEventListener (NetStatusEvent.NET_STATUS,doSO);
		}
		
		private function doSO (e:NetStatusEvent):void
		{
			good=e.info.code == "NetConnection.Connect.Success";
			if (good)
			{
				//Set up shared object
				text_so=SharedObject.getRemote("test",nc.uri,false);
				text_so.connect (nc);
				text_so.addEventListener (SyncEvent.SYNC,checkSO);
			}
		}
		
		private function checkSO (e:SyncEvent):void
		{
			for (var chng:uint; chng<e.changeList.length; chng++)
			{
				switch (e.changeList[chng].code)
				{
					case "clear" :
						break;
					
					case "success" :
						trace (text_so.data.msg);
						break;
					
					case "change" :
						textArea.appendText (text_so.data.msg + "\n");
						break;
				}
			}
		}
		
		private function sendMsg (e:MouseEvent):void
		{
			text_so.setProperty ("msg",textInput.text);
			textArea.appendText (textInput.text + "\n");
		}
	}
}
